package com.mdxq.mall.sale.controller;

import cn.hutool.core.util.StrUtil;
import com.mdxq.mall.response.R;
import com.mdxq.mall.sale.entity.CouponProductCategoryRelation;
import com.mdxq.mall.sale.entity.CouponProductRelation;
import com.mdxq.mall.sale.service.CouponProductCategoryRelationService;
import com.mdxq.mall.sale.service.CouponProductRelationService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import com.mdxq.mall.sale.entity.Coupon;
import com.mdxq.mall.sale.service.CouponService;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * 优惠券表 控制层。
 *
 * @author zdy
 * @since 2025-01-12
 */
@RestController
@RequestMapping("/api/v1/coupon")
public class CouponController {

    @Autowired
    private CouponService couponService;

    /**
     * 添加优惠券表。
     *
     * @param coupon 优惠券表
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("create")
    public R save(@RequestBody Coupon coupon) {
        return R.ok(couponService.save(coupon));
    }

    /**
     * 根据主键删除优惠券表。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @PostMapping("delete/{id}")
    public R remove(@PathVariable Long id) {
        return R.ok(couponService.removeById(id));
    }

    /**
     * 根据主键更新优惠券表。
     *
     * @param coupon 优惠券表
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PostMapping("update/{id}")
    public R update(@PathVariable Long id,@RequestBody Coupon coupon) {
        return R.ok(couponService.updateById(coupon));
    }

    /**
     * 查询所有优惠券表。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public List<Coupon> list() {
        return couponService.list();
    }

    @Autowired
    private CouponProductRelationService productRelationService;
    @Autowired
    private CouponProductCategoryRelationService categoryRelationService;
    /**
     * 根据优惠券表主键获取详细信息。
     *
     * @param id 优惠券表主键
     * @return 优惠券表详情
     */
    @GetMapping("getInfo/{id}")
    public R getInfo(@PathVariable Long id) {
        Coupon coupon = couponService.getById(id);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("coupon_id", id);
        coupon.setProductRelationList(productRelationService.listAs(queryWrapper, CouponProductRelation.class));
        queryWrapper.clear();
        queryWrapper.eq("coupon_id", id);
        coupon.setProductCategoryRelationList(categoryRelationService.listAs(queryWrapper, CouponProductCategoryRelation.class));
        return R.ok(coupon);
    }

    /**
     * 分页查询优惠券表。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public R page(Page<Coupon> page,String name,Integer type) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StrUtil.isNotBlank(name)){
            queryWrapper.eq("name",name);
        }
        if (type!=null){
            queryWrapper.eq("type",type);
        }
        return R.ok(couponService.page(page,queryWrapper));
    }

}
